package com.massyframework.beanskin.license.signature.wizard;

import java.util.Optional;

import javafx.geometry.Pos;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class PasswordUtils {

	/**
	 * 打开密码输出对话框
	 * @param title 标题
	 * @param headerText 头部提示信息
	 * @param inputText 输入信息
	 * @return {@link Optional}
	 */
	public static Optional<String> openPasswordDialog(String title, String headerText, String inputText){
		Dialog<String> dialog = new Dialog<>();
	    dialog.setTitle(title);
	    dialog.setHeaderText(headerText);
	    
	    final PasswordField pwd = new PasswordField();
	    pwd.setPrefWidth(300);
	    
	    dialog.setGraphic(new Circle(15, Color.RED)); // Custom graphic
	    dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);

	    HBox content = new HBox();
	    content.setAlignment(Pos.CENTER_LEFT);
	    content.setSpacing(10);
	    content.getChildren().addAll(new Label(inputText), pwd);
	    dialog.getDialogPane().setContent(content);
	    dialog.setResultConverter(dialogButton -> {
	        if (dialogButton == ButtonType.OK) {
	            return pwd.getText();
	        }
	        return null;
	    });
	    
	    dialog.setOnShown((event) -> {
	    	pwd.requestFocus();
	    });

	   return dialog.showAndWait();
	}
}
